package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.fragment.app.FragmentActivity;

import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.circle.BlogLikeInfo;
import com.zy.openchat.core.model.circle.BlogUserDateInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.Common;

import java.util.Map;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.utils.KLog;


public class MyFollowViewModel extends PagedViewModel<MyFollowItemViewModel, BlogUserDateInfo> {
    public int mType;
    public long userId;
    private MyFollowPagerGroupViewModel myFollowPagerGroupViewModel;
    public FragmentActivity activity;

    public void setType(int type) {
        this.mType = type;
    }

    public MyFollowViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    public void requestData(int page) {
        TgResultBlock<BlogLikeInfo> block = new TgResultBlock<BlogLikeInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, BlogLikeInfo obj) {
                finishRefreshAndLoad(page, obj == null ? null : obj.user_dates, MyFollowItemViewModel.class);
                totalData.clear();
                totalData.addAll(observableList);
                doSearch();
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                finishRefreshAndLoad(page, null, MyFollowItemViewModel.class);
            }
        };
        if (mType == 0) {
            TelegramManager.getTelegramManager().getBlogFollows(getApplication(), userId, PAGE_COUNT(), (page - 1) * PAGE_COUNT(), block);
        } else {
            TelegramManager.getTelegramManager().getBlogFans(getApplication(), userId, PAGE_COUNT(), (page - 1) * PAGE_COUNT(), block);
        }

    }

    private ObservableList<MultiItemViewModel> totalData = new ObservableArrayList<>();

    public void doSearch() {
        String keyword = myFollowPagerGroupViewModel.search.get();
        KLog.d("keyword==" + keyword);
        observableList.clear();
        if (TextUtils.isEmpty(keyword)) {
            if (!Common.isEmpty(totalData)) {
                observableList.addAll(totalData);
            }
        } else {
            if (!Common.isEmpty(totalData)) {
                for (MultiItemViewModel dateInfo : totalData) {
                    BlogUserDateInfo entity = ((MyFollowItemViewModel) dateInfo).entity.get();
                    UserInfo userInfo = UserInfo.getUserInfo(entity.user_id);
                    if (userInfo != null && !TextUtils.isEmpty(userInfo.displayName()) && userInfo.displayName().contains(keyword)) {
                        observableList.add(dateInfo);
                    }
                }
            }
        }
    }

    @Override
    public int PAGE_COUNT() {
        return 1000;
    }

    @Override
    public int initContentView() {
        return R.layout.item_my_follow;
    }

    public void setData(Bundle bundle) {
        if (bundle != null) {
            mType = bundle.getInt("type");
            userId = bundle.getLong("userId");
        }
    }

    public void setVm(MyFollowPagerGroupViewModel myFollowPagerGroupViewModel) {
        this.myFollowPagerGroupViewModel = myFollowPagerGroupViewModel;
    }

    public void setActivity(FragmentActivity activity) {
        this.activity = activity;
    }
}
